package training;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_6_5_Training {
    //自行车
//    public static Integer n;
//    public static Integer m;
//    public static final long INF = Long.MAX_VALUE-1;
//    public static long[] dist ;
//    public static long[][] v ;
//    public static boolean[] sti ;
//    public static long[] nums ;
//
//    public static void main(String[] args) {
//        //System.out.println(Arrays.toString(sti));
//        Scanner scanner = new Scanner(System.in);
//        n = scanner.nextInt();
//        m = scanner.nextInt();
//        dist = new long[n+1]; v = new long[n+1][n+1];
//        sti = new boolean[n+1]; nums = new long[n+1];
//        for (int i = 0; i <= n; i++) {
//            Arrays.fill(v[i],INF);
//        }
//        for (int i = 1; i <= m; i++) {
//            int x = scanner.nextInt();
//            int y = scanner.nextInt();
//            long z = scanner.nextInt();
//            v[x][y] = v[y][x] = Math.min(v[x][y],z);
//        }
//        long ret = prim();
//        if (ret == INF){
//            System.out.println(-1);
//        }else {
//            System.out.println(ret);
//        }
////        for (int i = 2; i <= n; i++) {
////            System.out.print(nums[i]+" ");
////        }
////        if(ret == INF){
////            System.out.println(-1);
////        }else {
////            long max = 0;
////            for (int i = 1; i <= n; i++) {
////                max = Math.max(nums[i],max);
////            }
////            System.out.println(max);
////        }
//    }
//
//    public static long prim(){
//        long ret = 0;
//        Arrays.fill(dist,INF);
//        dist[1] = 0;
//
//        for (int i = 0; i < n; i++) {
//            int idx = -1;
//            for (int j = 1; j <= n; j++) {
//                if(!sti[j] && (idx==-1 || dist[idx]>dist[j])){
//                    idx = j;
//                }
//            }
//            if(dist[idx] == INF){
//                return INF;
//            }
//            ret += dist[idx];
//            sti[idx] = true;
//            if(idx!=1){
//                nums[idx] = dist[idx];
//            }
//            for (int j = 1; j <= n; j++) {
//                if(sti[j]==false) {
//                    dist[j] = Math.min(dist[j], v[idx][j]);
//                }
//            }
//        }
//        return ret;
//    }
    static int n;
    static int m;
    public static final int INF = 0x3f3f3f3f;
    public static int[] dist;
    public static boolean[] sti;
    public static int[][] v;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        dist = new int[n+1]; sti = new boolean[n+1];
        v = new int[n+1][n+1];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(v[i],INF);
        }
        for (int i = 1; i <= m; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int z = scanner.nextInt();
            v[x][y] = v[y][x] = Math.min(z,v[x][y]);
        }

        int ret = prim();
        if(ret == INF){
            System.out.println(-1);
        }else {
            System.out.println(ret);
        }
    }

    public static int prim(){
        Arrays.fill(dist,INF);
        dist[1] = 0;
        int ret = 0;

        for (int i = 0; i < n; i++) {
            int idx = -1;
            for (int j = 1; j <= n; j++) {
                if(!sti[j] && (idx == -1 || dist[j]<dist[idx])){
                    idx = j;
                }
            }

            if(dist[idx] == INF){
                return INF;
            }
            ret += dist[idx];
            sti[idx] = true;
            for (int j = 1; j <= n; j++) {
                dist[j] = Math.min(dist[j], v[idx][j]);
            }
        }
        return ret;
    }

}





